package server;
import java.net.*;
import java.io.*;
import java.util.*;

 //				"/tell someone msgs": send "msgs" to the person "someone" only. 
 //       Student1> /tell Student2 Hello, Student2.
 //       Student1 told Student2: Hello, Student2.
 //       Student1> /tell Student2 Hello, Student2.   <-- if Student2 does not exist.
 //       Error: 'Student2' is not online. 
 //       Student1> /tell                             <-- if empty. 
 //       Error: No target was given. 


class tell extends ChatFormat
{
	private String someone = null;
	public tell(String line)
	{
		super(line);
	}

	public void execute(KKMultiServerThread server)
	{
		
		String[] names = msg.split(" ");
		someone = names[0];
		//System.out.println("someone : " + someone);
		//System.out.println("Length : " + names.length);
		if( names.length >1 )
		msg = names[1];
		else
		msg = "";
		for(int i=2;i< names.length;i++)
			msg = msg.concat(" "+names[i]);
		
		if(someone.compareTo("") == 0){
			String name = server.getUser();	
			PrintWriter out = ((KKMultiServerThread.ChatIO) KKMultiServerThread.getChatMap().get(name)).out;
			//       Error: 'Student2' is not online. 
			//out.println("Error: No target was given.");
			server.send_reply_client(out, "Error: No target was given." );	
		}
		else if( KKMultiServerThread.getChatMap().containsKey(someone) ){
			PrintWriter out = ((KKMultiServerThread.ChatIO) KKMultiServerThread.getChatMap().get(someone)).out;
			// Student1 told Student2: Hello, Student2.
			String name = server.getUser();
			//out.println(name + " told " + someone + ": " + msg); 
			server.send_reply_client(out, name + " told " + someone + ": " + msg );
			
		}else{
			String name = server.getUser();	
			PrintWriter out = ((KKMultiServerThread.ChatIO) KKMultiServerThread.getChatMap().get(name)).out;
			//       Error: 'Student2' is not online. 
			//out.println("Error: '" + someone +"' is not online.");
			server.send_reply_client(out, "Error: '" + someone +"' is not online." );	
			
		}
	}
}
